package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.Tenant;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.TenantService;
import com.dataseek.iot.sys.service.SysUserService;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.sys.vo.TenantVo;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolikit.common.util.EncodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @Autowired
    private DbService dbService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询租户列表
     *
     * @param companyName 公司名称
     * @param status 租户状态
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryTenant(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size, @RequestParam(value="companyName", required=false) String companyName, @RequestParam(value="status", required=false)  String status, @RequestParam(value="companyCode", required=false)  String companyCode) throws IOException {
        TenantVo tenantVo = tenantService.queryTenant(companyCode,companyName,status,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),tenantVo);
    }

    /**
     * 修改租户列表
     *
     * @param tenant 租户实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateTenant(@RequestBody Tenant tenant) throws IOException {
        Integer code  = tenantService.updateTenant(tenant);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建租户
     *
     * @param tenant 租户实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertTenant(@RequestBody Tenant tenant) throws IOException, SQLException {
        Integer code  = tenantService.insertTenant(tenant);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"公司编码字段重复，请重新输入!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        dbService.createDb("postgresql",tenant.getCompanyCode());
        dbService.createDbTables("postgresql",tenant);
        // TODO: 下面是给初始化的用户库表的用户表新增默认管理员用户
       //SysUserTable sysUserTable = new SysUserTable();
       //sysUserTable.setTenantCode("tenant.getCompanyCode()");
       //sysUserTable.setUserName(tenant.getTenantName());
       //sysUserTable.setUserPassword(EncodeUtil.passwordEncoder(tenant.getTenantPassword()));
       //sysUserTable.setRoleName("admin");
       //sysUserTable.setStatus("1");
       //userService.insertUser(sysUserTable);
        return responseResult;
    }


    /**
     * 删除租户
     *
     * @param tenantIds 租户ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteTenant(@RequestBody JSONObject tenantIds)  throws IOException {
        JSONArray jsonArray = tenantIds.getJSONArray("ids");
        List<Integer> ids = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = tenantService.deleteTenant(ids);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }





}
